package beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import entities.*;

import db.DBBookManager;
import db.DBLoanManager;
import db.DBUserManager;
import entities.*;

@Stateless(mappedName="Search")
public class SearchBean implements SearchManager {

	@Override
	public Object directSearch(String searchTerm, String searchType, Integer userID) {

		if ( searchType.equalsIgnoreCase("people") ) {
			ArrayList<User> users = new ArrayList<User>();
			// split term based on spaces. if spaces, then first name and last name
		    String[] fields = searchTerm.split(" ");
		    
		    
		    if ( fields.length > 1 ) {
	    		users = DBUserManager.searchUser(fields[0], fields[1], userID.intValue());
		    }
		    else if ( fields.length == 1 ){
		    	users = DBUserManager.searchUser(fields[0], null, userID.intValue());
		    }
		    
			// else search by email address, last name and first name
		    return users;
			
		}
		else if ( searchType.equalsIgnoreCase("cd") || searchType.equalsIgnoreCase("dvd") || searchType.equalsIgnoreCase("book") ) {
			// do a search based on title
			ArrayList<Item> records = new ArrayList<Item>();
	    	if ( userID == null )
	    		records = DBBookManager.searchResults(searchTerm, searchType);
	    	else 
	    		records = DBBookManager.searchResults(searchTerm, searchType, userID);
			return records;
		}
		return null;
	}

	
	
	@Override
	public ArrayList<Item> getItems(int userId, String copyStatus, String type, int maxNum) {
		// TODO Auto-generated method stub

		return DBBookManager.findOwnerItems( userId,  copyStatus,  type, maxNum);
	}

	public ArrayList<Item> getItems(int userId) {
		// TODO Auto-generated method stub

		return DBBookManager.findOwnerItems( userId );
	}

	@Override
	public ArrayList<Item> getLoanedItems(int borrowerId, int max) {
		// TODO Auto-generated method stub
		
		return DBLoanManager.getLoanedItems(borrowerId, max);
	}

	@Override
	public ArrayList<Item> getAllItems() {
		// TODO Auto-generated method stub
		return DBBookManager.getAllItems();
	}

	@Override
	public Item getItem(int masterId) {
		// TODO Auto-generated method stub
		return DBBookManager.getItem(masterId);
	}

}
